﻿function GetHtmlTableJson(TableID) {
    var HtmlTableJson = "";
    HtmlTableJson += "[";
    HtmlTableJson += GetTrJson($("#" + TableID));
    HtmlTableJson += "]";
    $("#HidHtmlTableJson").val(HtmlTableJson);
    $("#Btn_ExportRecipes").trigger("click"); //触发导出事件
}

//获得tr的json集合
function GetTrJson(oTd) {
    var HtmlTableJson = "";
    var RowIndex = 0;
    var StartCellIndex = 0;
    var MergeRowCount = 0;

    //层级处理
    if ($(oTd).children("table").length > 0) {
        oTd = $(oTd).children("table");
    }
    if ($(oTd).children("thead").length > 0 && $(oTd).children("tbody").length > 0) {
        oTd = $(oTd).children("thead,tbody");
    }
    if ($(oTd).children("thead").length > 0) {
        oTd = $(oTd).children("thead");
    }
    if ($(oTd).children("tbody").length > 0) {
        oTd = $(oTd).children("tbody");
    }

    $(oTd).children("tr").each(function () {
        var Table_Tr = "";
        Table_Tr += "{";
        Table_Tr += "\"RowIndex\":" + RowIndex + ","; //行索引
        Table_Tr += "\"CellS\":[";

        var CellIndex = 0;

        //采用合并单元格的索引
        if (StartCellIndex > 0) {
            CellIndex = StartCellIndex;
        }

        var Table_Td = "";
        $(this).children("td,th").each(function () {
            Table_Td += "{";

            //索引
            Table_Td += "\"CellIndex\":" + CellIndex + ",";

            //内容
            if ($(this).find("tr").length > 0) {
                Table_Td += "\"Content\":\"\",";
            }
            else {
                Table_Td += "\"Content\":\"" + $.trim($(this).text().toString()) + "\",";
            }
            //宽
            if ($(this).attr("colspan") != undefined && $(this).attr("colspan") != "") {
                Table_Td += "\"CellWidth\":" + $(this).attr("colspan") + ",";
            }
            else {
                Table_Td += "\"CellWidth\":1,";
            }

            //高
            if ($(this).attr("rowspan") != undefined && $(this).attr("rowspan") != "") {
                Table_Td += "\"CellHeight\":" + $(this).attr("rowspan") + ",";
                MergeRowCount = parseInt($(this).attr("rowspan"));  //仅限于一行中只有一个rowspan
                //StartCellIndex = parseInt($(this).attr("colspan")) - 1;
                StartCellIndex = 1; //暂时只支持第一列合并行
            }
            else {
                Table_Td += "\"CellHeight\":1,";
            }
            //内部集合
            if ($(this).find("tr").length > 0) {
                Table_Td += "\"RowS\":[";
                Table_Td += GetTrJson($(this)); //**********递归获取
                Table_Td += "]";
            }
            else {
                Table_Td += "\"RowS\":null";
            }
            Table_Td += "},";
            CellIndex += 1;
        });
        if (Table_Td.length > 0) {
            Table_Tr += Table_Td.substr(0, Table_Td.length - 1);
        }

        Table_Tr += "]";
        Table_Tr += "},";
        HtmlTableJson += Table_Tr;
        RowIndex += 1;

        //处理合并行结构
        if (MergeRowCount > 0) {
            MergeRowCount -= 1;
            if (MergeRowCount == 0) {
                StartCellIndex = 0;
            }
        }
    });
    if (HtmlTableJson.length > 0) {
        HtmlTableJson = HtmlTableJson.substr(0, HtmlTableJson.length - 1);
    }
    return HtmlTableJson;
}